package com.photoeditor.demo.ui.widget.image.frame;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;
import com.photoeditor.demo.model.image.frame.FrameIconManager;

import java.util.ArrayList;

/**
 * @dest 相框选择列表
 */
public class FrameListAdapter extends ArrayAdapter<Drawable> {

    //    private final int widthOrHeight;
    private LayoutInflater mInfl;

    private int mSelectedPosition = 0;

    private final int PADDING;
    ViewHolder holder;
    int[] frame_id = {
//            R.drawable.frame_m,R.drawable.frame_n,R.drawable.frame_o,R.drawable.frame_p,R.drawable.frame_q,
//            R.drawable.frame_r,R.drawable.frame_s,R.drawable.frame_t,R.drawable.frame_u,R.drawable.frame_v,
//            R.drawable.frame_w,R.drawable.frame_x,R.drawable.frame_y,R.drawable.frame_a,R.drawable.frame_b,
//            R.drawable.frame_c,R.drawable.frame_d,R.drawable.frame_f,R.drawable.frame_h,R.drawable.frame_j,
//            R.drawable.frame_k,R.drawable.frame_l,
    };
    private ArrayList<Bitmap> mDatas = new ArrayList<>();
    private final FrameIconManager mManager;

    public FrameListAdapter(Context context, int resource) {
        super(context, resource);

        mInfl = ((Activity) context).getLayoutInflater();
        PADDING = DeviceUtils.dip2px(getContext(), 13);
//        widthOrHeight =ImageHelper.dpToPx(mActivity.getResources(),69);
        mManager = new FrameIconManager(context);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInfl.inflate(R.layout.item_photoframe_list, null);
            ImageView frame = (ImageView) convertView.findViewById(R.id.photoframe);
            PhotoFrameBaseBgView baseBg = (PhotoFrameBaseBgView) convertView.findViewById(R.id.item_base_bg);
//            ImageView mBaseImage = (ImageView) convertView.findViewById(R.id.baseimage);
            holder = new ViewHolder();
            holder.mPhotoFrame = frame;
            holder.baseBg = baseBg;
//            holder.mBaseImage = mBaseImage;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mPhotoFrame.setTag(position);
        int currentPositionFrameResId = getCurrentPositionFrameResId(position);
        mManager.loadFrameThumbnail(holder.mPhotoFrame, currentPositionFrameResId, position);
//        Bitmap bitmap = mDatas.get(position);
//        Bitmap bitmapicon = ThumbnailUtils.extractThumbnail(bitmap, widthOrHeight, widthOrHeight);
//        holder.mPhotoFrame.setBackgroundDrawable(new BitmapDrawable(bitmapicon));
//        holder.mBaseImage.setImageBitmap(mBaseBitmap);
        if (mSelectedPosition == position) {
            holder.baseBg.setShow(true);
            holder.baseBg.setColor(getContext().getResources().getColor(R.color.accent_color));
        } else {
            holder.baseBg.setShow(false);
        }
        //处理UI细节
        if (position == getCount() - 1) {
            convertView.setPadding(PADDING, 0, PADDING, 0);
        } else {
            convertView.setPadding(PADDING, 0, 0, 0);
        }
        //为解决三星快速滑动错位问题
        holder.mPhotoFrame.requestLayout();
        return convertView;
    }

    public int getCurrentPositionFrameResId(int position) {
        if (frame_id.length != 0) {
            return frame_id[position];
        }
        return 0;
    }

    @Override
    public int getCount() {
        return frame_id.length;
    }

    @Override
    public Drawable getItem(int position) {
        if ((mManager != null)) {
            return mManager.getFrameDrawable(getContext().getResources(), frame_id[position]);
        }
        return null;
    }

    public void setData(ArrayList<Bitmap> data) {
        mDatas = data;
        notifyDataSetChanged();
    }

    /**
     * @param position    选中索引
     * @param convertView 选中view
     *                    给选中的item添加边框
     */
    public void setSelectedPosition(int position, View convertView) {
        mSelectedPosition = position;
        PhotoFrameBaseBgView backgroundView = (PhotoFrameBaseBgView) convertView.findViewById(R.id.item_base_bg);
        backgroundView.setShow(true);
        backgroundView.setColor(getContext().getResources().getColor(R.color.accent_color));

        ViewGroup vp = (ViewGroup) convertView.getParent();
        int child = vp.getChildCount();
        for (int i = 0; i < child; i++) {
            View v = vp.getChildAt(i);
            if (v != convertView) {
                PhotoFrameBaseBgView bv = (PhotoFrameBaseBgView) v.findViewById(R.id.item_base_bg);
                bv.setShow(false);
            }
        }
    }

    public void setSelectedPosition(int position) {
        mSelectedPosition = position;
        notifyDataSetChanged();
    }

    public void onDestory(boolean isActfinish) {
        if (mManager != null) {
            mManager.onDestory(isActfinish);
        }
    }

    public class ViewHolder {
        PhotoFrameBaseBgView baseBg;
        ImageView mPhotoFrame;
//        相框列表图片源
//        ImageView mBaseImage;
    }

}
